package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Consumer;
import com.fc.campusapplicationsystem.model.Logs;
import com.fc.campusapplicationsystem.model.Serviceman;
import com.fc.campusapplicationsystem.model.Superuser;
import com.fc.campusapplicationsystem.service.ConsumerService;
import com.fc.campusapplicationsystem.service.LogsService;
import com.fc.campusapplicationsystem.service.ServicemanService;
import com.fc.campusapplicationsystem.service.SuperuserService;
import com.fc.campusapplicationsystem.util.Constants;
import com.fc.campusapplicationsystem.util.IpAddressUtils;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import com.fc.campusapplicationsystem.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 贺哲
 * @2020-02-29 15:36
 */
@RestController
@RequestMapping("/loginController")
public class LoginController {

    @Autowired
    ConsumerService consumerService;

    @Autowired
    SuperuserService superuserService;

    @Autowired
    ServicemanService servicemanService;

    @Autowired
    LogsService logsService;

    /**
     * 用户注册
     * @param consumer 用户信息
     * @return 是否成功
     */
    @PostMapping("/Register")
    public ResponseOBJ<Integer> register(@RequestBody Consumer consumer){
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        try{
            Integer result = consumerService.add(consumer);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e){
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    /**
     * @param username
     * @param password
     * @param identity 管理员：admin,用户：user,维修人员：serviceman
     */
    @PostMapping("/Login")
    public ResponseOBJ<Map<String, Object>> Login(@RequestParam("username") String username,
                                                  @RequestParam("password") String password,
                                                  @RequestParam("identity") String identity
                                                  ) {
        Map<String, Object> map = new HashMap<>();
        ResponseOBJ<Map<String, Object>> responseOBJ = ResponseOBJ.getInstance();
        try {
            if (Constants.Identity.USER.equals(identity)) {
                Consumer consumer = consumerService.consumerLogin(username, password);
                map.put("identity", "user");
                map.put("consumer", consumer);
                WebUtil.getSession().setAttribute("consumer", consumer);
                logsService.add(Logs(WebUtil.getRequest()));
            } else if (Constants.Identity.ADMIN.equals(identity)) {
                Superuser superuser = superuserService.superuserLogin(username, password);
                map.put("identity", "admin");
                map.put("superuser", superuser);
                WebUtil.getSession().setAttribute("superuser", superuser);
                logsService.add(Logs(WebUtil.getRequest()));
            } else if (Constants.Identity.SERVICEMAN.equals(identity)) {
                Serviceman serviceman = servicemanService.servicemanLogin(username, password);
                map.put("identity", "serviceman");
                map.put("serviceman", serviceman);
                WebUtil.getSession().setAttribute("serviceman", serviceman);
                logsService.add(Logs(WebUtil.getRequest()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setMsg("用户名或密码错误");
        }
        responseOBJ.setData(map);
        return responseOBJ;
    }

    public static Logs Logs(HttpServletRequest request) {
        Logs logs = new Logs();
        logs.setIp(IpAddressUtils.getIpAddress(request));
        logs.setBrowser(IpAddressUtils.getBrowserName(request));
        logs.setLoginDate(new Date());
        /*int random = new Random().nextInt(100) + 1;
        logs.setIp("113.246.95." + random).setIpSource("湖南省|长沙市")
                .setBrowser(IpAddressUtils.getBrowserName(request))
                .setLoginDate(new Date());*/
        return logs;
    }


    /**
     * 获得登录的普通用户
     * @return
     */
    @GetMapping("/getLoginConsumer")
    public  ResponseOBJ<Consumer> getLoginConsumer(){
        ResponseOBJ<Consumer> responseOBJ = ResponseOBJ.getInstance();
        Consumer consumer = (Consumer) WebUtil.getSession().getAttribute("consumer");
        responseOBJ.setData(consumer);
        responseOBJ.setCode(ResponseOBJ.SUCCESS);
        responseOBJ.setMsg("当前用户为:"+consumer.getRealName());
        return responseOBJ;
    }



    /**
     * 获得登录的维修人员
     * @return
     */
    @GetMapping("/getLoginServiceman")
    public  ResponseOBJ<Serviceman> getLoginServiceman(){
        ResponseOBJ<Serviceman> responseOBJ = ResponseOBJ.getInstance();
        Serviceman serviceman = (Serviceman) WebUtil.getSession().getAttribute("serviceman");
        responseOBJ.setData(serviceman);
        responseOBJ.setCode(ResponseOBJ.SUCCESS);
        responseOBJ.setMsg("当前用户为:"+serviceman.getRealName());
        return responseOBJ;
    }



    /**
     * 获得登录的管理员用户
     * @return
     */
    @GetMapping("/getLoginSuperuser")
    public  ResponseOBJ<Superuser> getLoginSuperuser(){
        ResponseOBJ<Superuser> responseOBJ = ResponseOBJ.getInstance();
        Superuser superuser = (Superuser) WebUtil.getSession().getAttribute("superuser");
        responseOBJ.setData(superuser);
        responseOBJ.setCode(ResponseOBJ.SUCCESS);
        responseOBJ.setMsg("当前用户为:"+superuser.getRealName());
        return responseOBJ;
    }
}
